#include "groupinvite.h"
#include "ui_groupinvite.h"

groupInvite::groupInvite(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::groupInvite)
{
    ui->setupUi(this);
}

groupInvite::groupInvite(QTcpSocket * s, QString Id, QString gId, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::groupInvite)
{
    ui->setupUi(this);

    UIDesign::setButtonUI(ui->buttonCancel);
    UIDesign::setButtonUI(ui->buttonSubmit);
    UIDesign::setLineEditUI(ui->id);
    UIDesign::setTransparent(ui->label);
    UIDesign::setWindowUI(ui->centralwidget);
    setWindowFlags(Qt::FramelessWindowHint);
    setFixedSize(this->width(), this->height());
    this->setAttribute(Qt::WA_TranslucentBackground);

    sock = s;
    ID = Id;
    gID = gId;
}


groupInvite::~groupInvite()
{
    delete ui;
}

void groupInvite::recv(){


    QByteArray byteRequest;
    byteRequest = sock->readAll();
    QJsonParseError err;
    QJsonDocument document = QJsonDocument::fromJson(byteRequest, &err);
    if(err.error == QJsonParseError::NoError)
    {
        QJsonObject request = document.object();
        QJsonValue Rstatus = request.value("status");
        bool status = Rstatus.toBool();
        if(status)
        {
            QMessageBox::information(this, "提示", "已邀请。");
            this->close();
        }
        else
        {
            QMessageBox::warning(this, "警告", "申请不被认可！");
        }
    }
    else QMessageBox::warning(this, "警告", "服务器下传失败");
}

void groupInvite::on_buttonSubmit_clicked()
{

    tarID = ui->id->text();

    //申请建群格式：#07|myID|groupId|targetId

    disconnect(sock, SIGNAL(readyRead()), 0, 0);
    connect(sock, SIGNAL(readyRead()), this, SLOT(recv()));
    QJsonObject request;
    request.insert("order", 9);
    request.insert("myid", ID);
    QJsonObject data;
    data.insert("gd_id", gID);
    data.insert("target_id", tarID);
    request.insert("data", data);
    qDebug() << request;
    QJsonDocument document;
    document.setObject(request);
    QByteArray byteRequest = document.toJson();
    sock->write(byteRequest);

}

void groupInvite::on_buttonCancel_clicked()
{
    this->close();
}
